/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package x10.interfaces.cm15a.transmit;

import java.util.logging.Logger;

import x10.event.X10Event;
import x10.event.X10Remote;
import x10.queues.X10TransmitQueue;
import x10.utilities.CheckCode;
import x10.utilities.ChunkOfBytes;

/**
 *
 * @author Denny
 */
public class RFRemoteFactory extends X10DeviceTransmitter {
    Logger logger = Logger.getLogger(this.getClass().getName());

    X10Remote rfRemote;

    public RFRemoteFactory(X10TransmitQueue transmitQueue){
        super(transmitQueue);
    }

    public void transmit(X10Event device){
        try{
            rfRemote = (X10Remote)device;
        } catch (Exception e){
            logger.warning("Unable to cast X10Device to X10Remote");
        }

        ChunkOfBytes chunk = new ChunkOfBytes();

        byte addressCode = rfRemote.getAddressCode();
        byte remoteCode = rfRemote.getRemoteCommand().getByteValue();
        byte checkCode = CheckCode.getRemoteCheckCode(remoteCode, addressCode);

        byte[] bytes = new byte[5];
        bytes[0] = (byte) 0xEB;  //Transmit RF
        bytes[1] = (byte) 0x14;  //RF Remote Header
        bytes[2] = checkCode;
        bytes[3] = remoteCode;
        bytes[4] = addressCode;

        chunk.setBytes(bytes);
        chunk.setLength(5);
        chunk.setEmpty(false);

        transmitQueue.put(chunk);
    }
}
